const utils = {
  getLocalStorage(name, type) {
    let data = localStorage.getItem(name)
    if (type === 'number') {
      return Number(data)
    }
    if (type === 'boolen') {
      if (data === 'false') {
        data = false
      } else {
        data = true
      }
      return data
    }
    if (type === 'object') {
      data = JSON.parse(data)
      return data
    }
    return data
  },
  objKeySort: function(obj) {
    obj = obj || {}
    if (!obj.clearFull) {
      obj = this.filterNull(obj)
    }
    const newkey = Object.keys(obj).sort()
    let str = ''
    for (let i = 0; i < newkey.length; i++) {
      if (obj[newkey[i]]) {
        str += obj[newkey[i]]
      } else if (obj[newkey[i]] === 0) {
        str += obj[newkey[i]]
      }
    }
    return obj
  },
  filterNull(o) {
    for (const key in o) {
      if (o[key] === null || o[key] === undefined || o[key] === '') {
        delete o[key]
      }
      if (this.toType(o[key]) === 'string') {
        o[key] = o[key].trim()
      }
    }
    return o
  },
  toType(obj) {
    return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
  }
}
export default utils
